package com.example.demo.Service;

import com.example.demo.Entity.Collect;
import com.example.demo.Entity.Item;
import com.example.demo.Entity.User;
import com.example.demo.Repository.CollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CollectService {

    @Autowired
    private CollectRepository collectRepository;

    public List<Collect> getCollectsByUserId(Long userId) {
        return collectRepository.findByUserId(userId);
    }

    public void addCollect(Long itemId, Long userId) {
        Collect collect = new Collect();
        collect.setItem(new Item(itemId));
        collect.setUser(new User(userId));
        collect.setCollectedAt(LocalDateTime.now());
        collectRepository.save(collect);
    }

    public void removeCollect(Long itemId, Long userId) {
        collectRepository.deleteByItemIdAndUserId(itemId, userId);
    }

}